'use strict';
var router = require('express').Router();
var AV = require('leanengine');
var request = require('request');
var async = require('async');
// `AV.Object.extend` 方法一定要放在全局变量，否则会造成堆栈溢出。
// 详见： https://leancloud.cn/docs/js_guide.html#对象
var Todo = AV.Object.extend('Todo');

//项目主页
router.get('/', function(req, res) {
    res.render('index', {});
});

//augular项目主页
router.get('/augular', function(req, res, next) {
    res.render('augular',{});
});

//查询Name内容
router.get('/Dome', function(req, res, next) {
    res.render('Dome',{
        user:'土豪'
    });
});

//Dome读取数据接口
router.get('/Dome/plus', function(req, res, next) {
    console.log(1);
    var query = new AV.Query('Augular_Data');
    query.find().then(function(results) {
        console.log(1);
        var messageList =[];
        for(var i=0;i<results.length;i++){
            var obj={
                action:results[i].get('action'),
                done:results[i].get('done'),
                id:results[i].get('objectId')
            };
            messageList.push(obj);
        }
        console.log(messageList);
        res.json(
            messageList
        );

    }, function(err) {
        console.log(err)
    });
});

//Dome添加数据接口
router.get('/Dome/addTo', function(req, res, next) {
    var Todo = new AV.Object('Augular_Data');
    Todo.set('action', req.query.action);
    Todo.save().then(function () {
        res.json({
            code:'100',
            frequency:'添加成功'
        })
    }, function (error) {
        console.log(error);
        res.json({
            code:'101',
            frequency:'添加失败'
        })
    })
});

//Dome修改数据接口
router.get('/Dome/modify', function(req, res, next) {
    var modify = AV.Object.createWithoutData('Augular_Data', req.query.id);
    console.log(modify)
});


module.exports = router;